import {
  getArticles,
  getArticle,
  createArticle,
  updateArticle,
  delArticle
} from "../api/article";

export default {
  namespaced: true,
  state: {
    tableData: [],
    page: 1,
    size: 5,
    item: {}
  },
  mutations: {
    SET_TABLE_DATA(state, data) {
      state.tableData = data;
    },
    SET_ITEM(state, data) {
      state.item = data;
    },
    SET_PAGE(state, data) {
      state.page = data;
    }
  },
  actions: {
    async getList({ commit, state }) {
      const res = await getArticles({
        page: state.page,
        size: state.size
      });
      commit("SET_TABLE_DATA", res.data);
    },
    async getItem({ commit }, id) {
      const res = await getArticle(id);
      commit("SET_ITEM", res.data);
    },
    async createArticle(context, data) {
      await createArticle(data)
    },
    async updateArticle(context, { id, data }) {
      await updateArticle(id, data)
    },
    async delArticle(context, id) {
      await delArticle(id)
    }
  },
};
